﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using AspNetCoreMVC.Models;
using AspNetCoreMVC.EfRepository;
using MyMvcSoft.RepuestParams;

namespace AspNetCoreMVC.Controllers
{
    public class AccountController : Controller
    {

        public IActionResult Index(){
            var products=new EfRepository<Product>();
            var product=products.table as IEnumerable<Product>;
            return View(product);
        }
        public IActionResult CreateOrEdit(int id){
            var a = new EfRepository<Product>();
            var b = a.table.Where(x=>x.Id==id).FirstOrDefault();
            return View(b);
        }
        [HttpPost]
        public IActionResult Save(ProductParams model){
            var ProductRep = new EfRepository<Product>();
            if (model.Id > 0)
            {
                var d = ProductRep.GetByid(model.Id);
                var entity = ProductRep.table.Where(x => x.Id == model.Id).FirstOrDefault();
                entity.ProductName = model.ProductName;
                entity.Supplier=model.Supplier;
                entity.Price=model.Price;
                entity.Stock=model.Stock;
                entity.Remarks = model.Remarks;
                ProductRep.Update(entity);
            }
            else
            {
                ProductRep.Insert
                (
                    new Product
                    {
                        ProductName = model.ProductName.Trim(),
                        Supplier = model.Supplier.Trim(),
                        Price = model.Price,
                        Stock=model.Stock,
                        Remarks = model.Remarks.Trim()
                    }
                );
            }
            return Ok();
        }
        public IActionResult Delete(int id){
            var accoutrep=new EfRepository<Product>();
            if (id!=0)
            {
                accoutrep.Delete(id);
            }
            return Ok();
        }
       
    }
}
